package com.catsic.core.depart.dao;

import java.util.List;

import javax.persistence.QueryHint;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;

import com.catsic.base.BaseDao;
import com.catsic.core.entity.Depart;

/**
 * @author yangyd
 */
public interface DepartDao extends BaseDao<Depart, String>{
	
	/**
	 * 通过机构ID查询该机构下的所有部门
	 * @param id
	 * @return
	 */
	@Query("select d from Depart d where d.organ.id = ?1 order by d.code asc")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public List<Depart> findByOrganId(String id);
	

	
	
	/**
	 * findByParentIdOrderByCodeDesc:查询父节点下所有子节点,按编码倒叙排列
	 * @author yangyudi
	 * @param id
	 * @return
	 */
	public List<Depart> findByParentIdOrderByCodeDesc(String id);
	
	/**
	 * findByParentIdAndOrganIdOrderByCodeDesc:通过机构Id与部门父节点Id.
	 * @author yangyd
	 * @param id
	 * @param organId
	 * @return
	 **/
	public List<Depart> findByParentIdAndOrganIdOrderByCodeAsc(String id,String organId);
	
	/**
	 * 通过父节点查询所有子节点
	 * @param id
	 * @return
	 */
	@Query("select d from Depart d where d.parent.id = ?1 order by d.code asc")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public List<Depart> findByPid(String id);
	
	/**
	 * 通过父节点查询所有子节点个数
	 * @param id
	 * @return
	 */
	@Query("select count(*) from Depart o where o.parent.id = ?1")
	public Integer findCountByPid(String id);
}
